<template>
  <div class="anual-editor">
    <PageTemplate>
      <div slot="content">
         <el-form
          :model="budgetInfo"
          label-position="right"
          label-width="150px"
          class="page-edit-colllapse-form"
          ref="budgetInfo">
          <el-row>
            <el-col :span="12">
              <el-form-item label="预算年度" prop="year" :rules="rules">
                  <el-input  class="form-input"  v-model="budgetInfo.year"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="差旅费" prop="travelExpense" :rules="rules">
                <el-input  class="form-input"  v-model="budgetInfo.travelExpense"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="会议费" prop="meetingExpense" :rules="rules">
                <el-input  class="form-input"  v-model="budgetInfo.meetingExpense"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="咨询费" prop="consultingExpense" :rules="rules">
                <el-input  class="form-input"  v-model="budgetInfo.consultingExpense"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
            <el-col :span="12">
              <el-form-item label="业务支持费" prop="businessSupportExpense" :rules="rules">
                <el-input  class="form-input"  v-model="budgetInfo.businessSupportExpense"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="设备费" prop="deviceExpense" :rules="rules">
                <el-input  class="form-input"  v-model="budgetInfo.deviceExpense"></el-input>
              </el-form-item>
            </el-col>
          <el-row>
            <el-col :span="12">
              <el-form-item label="软件费" prop="softwareExpense" :rules="rules">
                <el-input  class="form-input"  v-model="budgetInfo.softwareExpense"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <el-row class="row-empty-10"></el-row>
        <el-row>
           <el-button type="primary" @click="savaHandle">
            {{$t('cm.save')}}
          </el-button>
          <el-button type="primary" @click="back">
            {{$t('cm.return')}}
          </el-button>
        </el-row>
      </div>
    </PageTemplate>
  </div>
</template>

<script>
import { ALERT } from '@/utils/responseHandle'
import * as Constant from '@/utils/constant'
import PageTemplate from '@/components/common/PageTemplate'
import HasBtn from '@/mixins/HasBtn'
import { mapState } from 'vuex'
import {COMMON} from '@/utils/commonUtil'
import api_sri from '@/api/api_sri'
export default {
  components: {
    PageTemplate, 
  },
  props: {
    // add：新增；edit：编辑
    type: {
      type: String,
      default: "add"
    },
  },
  data () {
    return {
      budgetInfo: { 

      },
      rules:[{ required: true, message: '必填项',trigger: 'blur'}],
    }
  },
  watch: {
    
  },
  computed: {
    
  },
  created () {
    if(this.type == "edit") this.getData();
  },
  methods: {
    getData(){
       api_sri.sriGainBudgetInfo({
         id:this.$router.history.current.params.id
       }, result => {
        if (result.code === Constant.RT_SUCCESS) {
          this.budgetInfo = result.data.budgetInfo
        } else {
          ALERT.error(result.msg)
        }
      })

    },
    //返回
    back(){
      this.$router.go(-1);//返回上一层
    },
    //保存防抖
    savaHandle: COMMON.debounce('save'),
    // 保存
    save(){
      this.$refs.budgetInfo.validate((valid, error) => {
        if (valid) {
          const {
            year, 
            travelExpense, 
            meetingExpense,
            consultingExpense,
            businessSupportExpense,
            deviceExpense,
            softwareExpense,
          } = this.budgetInfo;
          if(this.type == "edit"){
            api_sri.sriGainBudgetUpdate({
              id:this.$router.history.current.params.id,
              year, 
              travelExpense, 
              meetingExpense,
              consultingExpense,
              businessSupportExpense,
              deviceExpense,
              softwareExpense,
            }, result => {
              if (result.code === Constant.RT_SUCCESS) {
                this.back();
              } else {
                ALERT.error(result.msg)
              }
              
            })
        }else{
          api_sri.sriGainBudgetAdd({
              year, 
              travelExpense, 
              meetingExpense,
              consultingExpense,
              businessSupportExpense,
              deviceExpense,
              softwareExpense,
            }, result => {
              if (result.code === Constant.RT_SUCCESS) {
                this.back();
              } else {
                ALERT.error(result.msg)
              }
              
            })

        }
          
          

        }
      });
      

    },
   
  },
}
</script>

<style lang="scss">
.anual-editor{
  .el-form{
    background: #fff;
    padding-top: 20px;
  }
}
</style>
